package com.dt.dubai.mech.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.mech.entity.MechMarketingCouponEntity;
import com.dt.dubai.mech.controller.innate.MechMarketingCouponInnateAbs;
import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.utile.DateTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
* 商户优惠券记录表
*/

@Controller
@RequestMapping(value = "/mech/mechMarketingCoupon")
public class MechMarketingCouponController extends MechMarketingCouponInnateAbs {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MechMarketingCouponEntity inEntity, final Long userId) {
        inEntity.setDelSign(false);
    }

    @Override
    protected void userModEntity(MechMarketingCouponEntity inEntity, final Long userId) {
    }

    @Override
    protected void userDelByKey(List<Long> recordIds, final Long userId) {
    }

    @Override
    protected void userQueByKey(Long recordId, Map<String, Object> map, final Long userId) {
        if (null != map && map.size() > 0) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            map.remove("adminLoginRecordId");
            map.remove("retailCommodityRecordId");
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    @Override
    protected void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId) {
        for (Map<String, Object> map : resultMap) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            Short commodityType = null == map.get("commodityType") ? -1 :  (Short) map.get("commodityType");
            switch (commodityType) {
                case 1:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "电商");
                    break;
                case 2:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "检测");
                    break;
                case 3:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "计量");
                    break;
                default:
                    map.put("commodityType", -1);
                    map.put("commodityTypeStr", "未知");
            }
            Short couponPublishType = null == map.get("couponPublishType") ? -1 :  (Short) map.get("couponPublishType");
            switch (couponPublishType) {
                case 1:
                    map.put("couponPublishType", couponPublishType);
                    map.put("couponPublishTypeStr", "领取");
                    break;
                case 2:
                    map.put("couponPublishType", couponPublishType);
                    map.put("couponPublishTypeStr", "赠送");
                    break;
                case 3:
                    map.put("couponPublishType", couponPublishType);
                    map.put("couponPublishTypeStr", "推送");
                    break;
                default:
                    map.put("couponPublishType", -1);
                    map.put("couponPublishTypeStr", "未知");
            }
            if (null != map.get("startUseTime"))
                map.put("startUseTime", DateTools.dateFormat((Date) map.get("startUseTime"), DateTools.DATE_FORMAT_YMD));
            if (null != map.get("endUseTime"))
                map.put("endUseTime", DateTools.dateFormat((Date) map.get("endUseTime"), DateTools.DATE_FORMAT_YMD));
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，用户代码请写入下方*/

    @RequestMapping(value = "/findListByUseStatus", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> findListByUseStatus(Boolean useStatus, final Long userId) {
        try {
            List<MechMarketingCouponEntity> list=mechMarketingCouponSer.findListByUseStatus(useStatus,userId);
            return new PageData().success(list, i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }


}